//
//  ALPListenAlbumTableViewCell.m
//  AlpFM
//
//  Created by KING on 16/1/27.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPListenAlbumTableViewCell.h"
#import "UIImageView+WebCache.h"
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
//屏幕高
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
@interface ALPListenAlbumTableViewCell()
@property (nonatomic, strong) UIImageView *headerView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;

@property (nonatomic, strong) UILabel *playTimes;
@property (nonatomic, strong) UILabel *longTime;
@property (nonatomic, strong) UILabel *button;

@end
@implementation ALPListenAlbumTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}


- (void)createSubViews{
    //图
    self.headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0.16*SCREEN_WIDTH, 0.16*SCREEN_WIDTH)];
    self.headerView.image = [UIImage imageNamed:@"girledg"];
    //    self.headerView.layer.masksToBounds = YES;
    //    self.headerView.layer.cornerRadius = 0.08*SCREEN_WIDTH;
    self.headerView.center = CGPointMake(SCREEN_WIDTH *0.15, 0.133*SCREEN_WIDTH);
    [self.contentView addSubview:self.headerView];
    
    //图上图
    UIImageView *upImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0.053*SCREEN_WIDTH, 0.053*SCREEN_WIDTH)];
    upImageView.image = [UIImage imageNamed:@"cell_sound_play_n"];
    upImageView.center = CGPointMake(0.08*SCREEN_WIDTH, 0.08*SCREEN_WIDTH);
    [self.headerView addSubview:upImageView];
    
    //下面的小灰条
    UIImageView *downImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0.267*SCREEN_WIDTH-2, SCREEN_WIDTH, 2)];
    downImageView.image = [UIImage imageNamed:@"bot"];
    [self.contentView addSubview:downImageView];
    
    //标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.267*SCREEN_WIDTH, 0.053*SCREEN_WIDTH, 0.667*SCREEN_WIDTH, 0.053*SCREEN_WIDTH)];
    self.titleLabel.text = @"作者标题";
    self.titleLabel.font = [UIFont systemFontOfSize:0.037*SCREEN_WIDTH];
//    self.titleLabel.backgroundColor = CLEAR;
    [self.contentView addSubview:self.titleLabel];
    
    //副标题
    self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.267*SCREEN_WIDTH, 0.107*SCREEN_WIDTH, 0.5*SCREEN_WIDTH, 0.053*SCREEN_WIDTH)];
    self.subtitleLabel.text = @"作者副标题";
    self.subtitleLabel.font = [UIFont systemFontOfSize:0.035*SCREEN_WIDTH];
//    self.subtitleLabel.backgroundColor = CLEAR;
    [self.contentView addSubview:self.subtitleLabel];
    
    
    //***************** 播放次数 *****************
    
    self.playTimes = [[UILabel alloc] initWithFrame:CGRectMake(0.106*SCREEN_WIDTH+ 0.21*SCREEN_WIDTH, 0.16*SCREEN_WIDTH, 0.133*SCREEN_WIDTH, 0.053*SCREEN_WIDTH)];
    [self.contentView addSubview:self.playTimes];
    self.playTimes.text = @"12765";
    self.playTimes.font = [UIFont systemFontOfSize:0.037*SCREEN_WIDTH];
//    self.playTimes.backgroundColor = CLEAR;
    
    //时长
    self.longTime = [[UILabel alloc] initWithFrame:CGRectMake(0.32*SCREEN_WIDTH+ 0.21*SCREEN_WIDTH, 0.16*SCREEN_WIDTH, 0.133*SCREEN_WIDTH, 0.053*SCREEN_WIDTH)];
    [self.contentView addSubview:self.longTime];
    self.longTime.text = @"18:23";
    self.longTime.font = [UIFont systemFontOfSize:0.037*SCREEN_WIDTH];
//    self.longTime.backgroundColor = CLEAR;
    
    //播放次数
    UIImageView *timesView = [[UIImageView alloc] initWithFrame:CGRectMake(0.06*SCREEN_WIDTH + 0.21*SCREEN_WIDTH, 0.173*SCREEN_WIDTH, 0.03*SCREEN_WIDTH, 0.03*SCREEN_WIDTH)];
    [self.contentView addSubview:timesView];
    timesView.image = [UIImage imageNamed:@"np_sound_playtimes_b@2x.png"];
    
    //时长
    UIImageView *longTimeView = [[UIImageView alloc] initWithFrame:CGRectMake(0.27*SCREEN_WIDTH+ 0.21*SCREEN_WIDTH, 0.167*SCREEN_WIDTH, 0.04*SCREEN_WIDTH, 0.04*SCREEN_WIDTH)];
    [self.contentView addSubview:longTimeView];
    longTimeView.image = [UIImage imageNamed:@"find_hotUser_sounds@2x.png"];
    
    //收藏按钮
//    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.button.frame = CGRectMake(0.85*SCREEN_WIDTH, 0.093*SCREEN_WIDTH, 30, 33);
////    self.button.backgroundColor = CLEAR;
//    [self.contentView addSubview:self.button];
//    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(Action:)];
    //设置轻扫方向
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    //添加
    [self.contentView addGestureRecognizer:swipe];
    
}

- (void)Action:(UISwipeGestureRecognizer *)btn{
    
    [self.MyDelegate popLastPage];
    //    NSLog(@"向右滑动手势执行");
}



//收藏
//- (void)buttonAction:(UIButton *)btn{
//    
//    NSMutableArray *array = [[DataBaseManager shareFMDB] selectALbumDataWhereUserName:@"xhf"];
//    NSMutableArray *titleArray = [NSMutableArray array];
//    for (HFLisionAlbumModel *model in array) {
//        [titleArray addObject:model.title];
//    }
//    
//    //如果包含就删除
//    if ([titleArray containsObject:self.model.title]) {
//        NSLog(@"删除%@", self.model.title);
//        [[DataBaseManager shareFMDB] deleteAlbumWithTitle:self.model.title];
//        [self.button setBackgroundImage:[UIImage imageNamed:@"find_album_unfav_n"] forState:UIControlStateNormal];
//        self.button.backgroundColor = CLEAR;
//    } else {
//        //如果包含就存储
//        NSLog(@"存储%@", self.model.title);
//        [[DataBaseManager shareFMDB] insertAlbum:self.model andUserName:@"xhf"];
//        [self.button setBackgroundImage:[UIImage imageNamed:@"find_album_fav_n"] forState:UIControlStateNormal];
//        self.button.backgroundColor = CLEAR;
//    }
//    
//}
//

- (void)setModel:(ALPAlbumModel *)model{
    _model = model;
    
    NSURL *url = [NSURL URLWithString:_model.albumCoverUrl290];
    [self.headerView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"girledg"]];
    
    self.titleLabel.text = _model.title;
    self.subtitleLabel.text = _model.intro;
    NSInteger wan = _model.playsCounts.intValue / 10000;
    NSInteger qian = _model.playsCounts.intValue %1000;
    self.playTimes.text = [NSString stringWithFormat:@"%ld.%ld万", (long)wan,(long)qian / 100];
    if (wan / 1000) {
        self.playTimes.text = @"千万+";
    }
    self.longTime.text = [NSString stringWithFormat:@"%@", _model.tracksCounts];
    
    
    //***************初始的时候设置按钮***************
//    NSMutableArray *array = [[DataBaseManager shareFMDB] selectALbumDataWhereUserName:@"xhf"];
//    NSMutableArray *titleArray = [NSMutableArray array];
//    for (HFLisionAlbumModel *model in array) {
//        [titleArray addObject:model.title];
//    }
    //    NSLog(@"%@", titleArray);
    //如果包含就显示已存
//    if ([titleArray containsObject:self.model.title]) {
//        //        NSLog(@"PP%@", self.model.title);
//        [self.button setBackgroundImage:[UIImage imageNamed:@"find_album_fav_n"] forState:UIControlStateNormal];
//        self.button.backgroundColor = CLEAR;
//    } else {
//        //如果不包含就显示未存
//        //        NSLog(@"ss%@", self.model.title);
//        [self.button setBackgroundImage:[UIImage imageNamed:@"find_album_unfav_n"] forState:UIControlStateNormal];
//        self.button.backgroundColor = CLEAR;
//    }
    //***********************************************
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
